import "babel-polyfill";
import Vue from 'vue'
import Vuex from 'vuex'
import FastClick from 'fastclick'
import {WechatPlugin, AjaxPlugin, LoadingPlugin, ToastPlugin, AlertPlugin} from 'vux'
import router from './router'
import App from './App'
import 'lib-flexible/flexible'
import { currency } from './util/currency'
import { cookie } from 'vux'
import axios from 'axios'
// import 'font-awesome/css/font-awesome.css'
/**
 * 加载插件
 */
Vue.use(Vuex)
Vue.use(WechatPlugin)
Vue.use(AjaxPlugin)
Vue.use(LoadingPlugin)
Vue.use(ToastPlugin)
Vue.use(AlertPlugin)
Vue.use(cookie)


// 价格格式化过滤器
Vue.filter("currency", currency)


/**
 * 创建 axios 实例配置*/
// const $axios = axios.create({
//   baseURL: 'http://192.168.1.4/sup/index.php?s=/shop',
//   timeout: 5000,
//   headers: {
//       'Content-Type': 'application/x-www-form-urlencoded',
//       // 'sessionId':  Lockr.get("sessionId"),
//       // 'authKey': Lockr.get("authKey"),
//   }
// });


/**
 * 设置vuex
 */

const store = new Vuex.Store({})
store.registerModule('vux', {
  state: {
    isLoading: false,
    showBack: true,
    title: '',
    number: '',
    key:'',
    iv:'',
    greenRightNum:0,
    preventGoBack:false,
    urlBack:''
  },
  mutations: {
    updateLoading (state, payload) {
      state.isLoading = payload.isLoading
    },
    updateShowBack (state, showBack) {
      state.showBack = showBack
    },
    updateTitle (state, title) {
      state.title = title
    },
    updateNumber (state, number){
      let num = ''
      if(number <= 0){
        state.number = num
      }else {
        num = number.toString()
        state.number = num
      }

    },
    updateKey (state, key) {
      state.key = key
    },
    updateIv (state, iv) {
      state.iv = iv
    },
    updateGreenRightNum (state, greenRightNum) {
      state.greenRightNum = greenRightNum
    },
    updatePreventGoBack (state, preventGoBack) {
      state.preventGoBack = preventGoBack
    },
    updateUrlBack (state, urlBack) {
      state.urlBack = urlBack
    }
  }
})
/**
 * 公用组件
 */
Vue.prototype.updateTitle = function (value) {
  this.$store.commit('updateTitle', value)
}
Vue.prototype.updateGreenRightNum = function (value) {
  this.$store.commit('updateGreenRightNum', value)
}
Vue.prototype.updatePreventGoBack = function (value) {
  this.$store.commit('updatePreventGoBack', value)
}
Vue.prototype.updateUrlBack = function (value) {
  this.$store.commit('updateUrlBack', value)
}
/**
 *  日志输出开关
 */
Vue.config.productionTip = true
/**
 *  点击延迟
 */
import './config/rem'
if ('addEventListener' in document) {
    document.addEventListener('DOMContentLoaded', function() {
        FastClick.attach(document.body);
    }, false);
}
router.beforeEach((to, from, next) => {
  store.commit('updateLoading', {isLoading: true})
  let vue = this;
  //NProgress.start();
  if (to.path == '/login') {
    // sessionStorage.removeItem('uid');
    // sessionStorage.removeItem('key');
    // sessionStorage.removeItem('iv');
    // sessionStorage.removeItem('access_token');
    cookie.remove('uid');
    cookie.remove('gid');
    cookie.remove('goodsChecked');
    cookie.remove('key');
    cookie.remove('iv');
    cookie.remove('access_token');
  }
  next();
  // let uid = JSON.parse(cookie.get('uid'));
  // if (!uid && to.path != '/login') {
  //   next({ path: '/login' })
  // } else {
  //   next()
  // }
})
router.afterEach((to,from,next) => {
  store.commit('updateLoading', {isLoading: false})
  console.log(window.scrollTop)
});
// axios.interceptors.response.use(
//   response => {
//       return response;
//   },
//   error => {
//       if (error.response) {
//         alert(1);
//         console.log(error.response)
//           if(error.response.status > 3000 &&  error.response.status < 4000){
//             cookie.set('uid','');
//             cookie.set('key','');
//             cookie.set('iv','');
//             cookie.set('access_token','');
//             router.replace({
//               path: '/login',
//               query: {redirect: router.currentRoute.fullPath}
//             })
//           }
//       }
//       return Promise.reject(error.response.data)   // 返回接口返回的错误信息
// });

/**
 *  创建实例
 */
new Vue({
  store,
  router,
  render: h => h(App)
}).$mount('#app-box')
